<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\Area;
use app\common\model\ProductCate;
use think\App;
use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\facade\View;
use think\response\Json;

class UserDiscount extends Base
{

    //主键
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\UserProductDiscount';

    protected $hasOne = ['user', 'product'];

    protected $append = ['status_text'];

    public function index()
    {
        return $this->setTable();
    }

    //自动构建table,按需修改
    public function setTable()
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'title' => '全选'])
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'user_nickname', 'title' => '会员', 'templet' => '<div>{{d.user.nickname}}</div>'])
            ->addRow(['field' => 'user_mobile', 'title' => '会员手机号', 'templet' => '<div>{{d.user.mobile}}</div>'])
            ->addRow(['field' => 'product_title', 'title' => '商品名称', 'width' => 200, 'templet' => '<div>{{d.product.title}}</div>'])
            ->addRow(['field' => 'amount', 'title' => '抵扣金额'])
            ->addRow(['field' => 'create_time', 'title' => '赠送时间', 'width' => 110])
            ->addRow(['field' => 'expire_time', 'title' => '过期时间', 'width' => 110])
            ->addRow(['field' => 'use_time', 'title' => '使用时间', 'width' => 110])
            ->addRow(['field' => 'status_text', 'title' => '状态'])
            ->addRow(['field' => 'explain', 'title' => '使用说明'])
            ->addRow(['field' => 'sn', 'title' => '编号', 'width' => 150])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 80]);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch())
            ->fetch();
    }

    //自动构建Search，按需修改
    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['uid', '会员编号/用户名/手机号', 'input', '', '\app\common\model\User', 'code|account|mobile,id'])
            ->setSearch(['pid', '商品名称', 'input', '', '\app\common\model\Product', 'title,id'])
            ->setSearch(['status', '状态', 'select', [1 => '未使用', 2 => '已使用', 3 => '已过期']]);

    }
}
